package com.wifi.eshipewifi;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wifi.BaseApplication;
import com.wifi.imp.MessageListener;
import com.wifi.imp.MessageSend;
import com.wifi.utils.WifiUtils;
import com.wifi.view.BlackConnetUserAdapter;

public class BlackConnetAcitvity extends AppCompatActivity implements View.OnClickListener,MessageListener {
    LinearLayout ll_top_blacklist;
    LinearLayout ll_background;
    TextView tv_top_blacklist;
    ImageButton ib_back;
    TextView tv_include_tittle;
    TextView tv_black;
    ProgressBar pb_conn;
    private PullToRefreshListView pull_friend_treasure;
    public BlackConnetUserAdapter adapter;
    private BaseApplication application;
    private String TAG="BlackConnetAcitvity";
    private boolean isRefresh=false;//下拉刷新
    String macAddress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_black_connet_acitvity);
        application = (BaseApplication) getApplication();


        tv_black = (TextView) findViewById(R.id.tv_black);
        pb_conn = (ProgressBar) findViewById(R.id.pb_conn);
        pull_friend_treasure = (PullToRefreshListView) findViewById(R.id.pull_mac);
        pull_friend_treasure.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        pull_friend_treasure.getLoadingLayoutProxy(false, true).setPullLabel("加载");
        pull_friend_treasure.getLoadingLayoutProxy(false, true).setRefreshingLabel("加载1");
        pull_friend_treasure.getLoadingLayoutProxy(false, true).setReleaseLabel("加载2");
        pull_friend_treasure.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉刷新
                application.sendMsg("$scan_allowlist;");
                isRefresh=true;
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //上拉加载更多
            }
        });

        WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifiMan.getConnectionInfo();
        macAddress=info.getMacAddress();// 获得本机的MAC地址

        ll_top_blacklist = (LinearLayout) findViewById(R.id.ll_top_blacklist);
        ll_background = (LinearLayout) findViewById(R.id.ll_background);
        tv_include_tittle = (TextView) findViewById(R.id.tv_include_tittle);
        tv_include_tittle.setText("白名单");
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        ib_back.setOnClickListener(this);
    }

    @Override
    protected void onResume() {//每个activity都要加这一句
        super.onResume();
        if(application==null)
          application = (BaseApplication) getApplication();
        application.setMsgListener(this);
        application.sendMsg("$scan_allowlist;");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                this.finish();
                break;
        }
    }

    @Override
    public void MessageGetListener(String msg) {
        try {
            if                                                                                                                                                                                                       (msg != null) {
                String s = msg;
                application.makeToast(BlackConnetAcitvity.this,s,false);
                if (s.trim().length() > 0) {
                    Log.i(TAG, "mhandler接收到obj=" + s);
                    String s1[]=s.split(";");
                    if(s1[0].indexOf("scan_allowlist")!=-1){//查询mac地址
                        application.makeToast(BlackConnetAcitvity.this,s1[1],false);
                        ll_background.setVisibility(View.GONE);//空背景
                        if(s1.length>1){
//                        String s="$scan_useres;b0:e2:35:95:3f:82,RedmiNote3;38:bc:1a:eb:2b:73,Meizu;";
//                        $scan_blacklist;b0:e2:35:95:3f:82, RedmiNote3,38:bc:1a:eb:2b:73,Meizu;\r\n
                            if(s1[1].indexOf("no")!=-1){
                                ll_background.setVisibility(View.VISIBLE);//空背景
                                pb_conn.setVisibility(View.GONE);
                                tv_black.setText("没有白名单");
                                //断线重连
                            }else {
                                adapter = new BlackConnetUserAdapter(BlackConnetAcitvity.this,s1,application,macAddress);
                                pull_friend_treasure.setAdapter(adapter);
                            }
                        }
                        if(isRefresh){
                            pull_friend_treasure.onRefreshComplete();
                            isRefresh=false;
                        }
//                        $scan_blacklist;no;
                    }else if(s1[0].indexOf("mac_recover")!=-1){
                        application.makeToast(BlackConnetAcitvity.this,"路由器重新连接中……",true);
                        //断线重连
                        String ssid = application.getSsid();
                        String password = application.getPassword();
                        WifiUtils wifiUtils=new WifiUtils(BlackConnetAcitvity.this);
                        wifiUtils.WifiRebootForSetName(BlackConnetAcitvity.this,application,ssid,password);
                    }
                }
            }
            Log.i(TAG, "mhandler接收到msg" + msg);
        } catch (Exception ee) {
            Log.i(TAG, "加载过程出现异常");
            ee.printStackTrace();
        }
    }
}
